package ink.tiptop.utils.date;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.sql.Timestamp;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.temporal.TemporalAdjusters;
import java.util.Date;

/**
 * @author Tiptop_ZJF
 * @ClassName: TimeUtils
 * @DESC 3.0.0时间工具类
 * @Date: 2019/8/3 11:05   DateBaseHolder.Pattern  DateBaseHolder.Pattern
 * @since JDK 1.8
 */

@Slf4j
public class DateHolder {

    /**
     * 当前时间对象
     *
     * @return
     */
    public static LocalDateTime now() {
        return LocalDateTime.now();
    }


    /**
     * 获取当前时间-格式为yyyyMMdd
     *
     * @return
     */
    public static String dateTime() {
        return now().format(DateBaseHolder.getFormatter(DateBaseHolder.Pattern.YMD));
    }

    /**
     * 获取当前时间-格式为yyyyMMdd
     *
     * @return
     */
    public static String formatDate(Date date, DateBaseHolder.Pattern pattern) {
        if (date == null) return "";
        LocalDateTime localDateTime = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
        return localDateTime.format(DateBaseHolder.getFormatter(pattern));
    }

    /**
     * 获取当前时间-带格式化
     *
     * @return
     */
    public static String dateTime(DateBaseHolder.Pattern pattern) {
        return now().format(DateBaseHolder.getFormatter(pattern));
    }

    /**
     * 获取当前月份的最后一天
     *
     * @param pattern
     * @return
     */
    public static String lastDayOfMonth(DateBaseHolder.Pattern pattern) {
        //本月的最后一天 2019-08-31T14:29:54.666
        LocalDateTime lastDayInMonth = now().with(TemporalAdjusters.lastDayOfMonth());
        return lastDayInMonth.format(DateBaseHolder.getFormatter(pattern));
    }

    public static LocalDateTimeHolder formDate(Date date) {
        LocalDateTime localDateTime = dateIsDate(date);
        return new LocalDateTimeHolder(localDateTime);
    }

    public static LocalDateTimeHolder formDate(Timestamp date) {
        LocalDateTime localDateTime = dateIsTimeStamp(date);
        return new LocalDateTimeHolder(localDateTime);
    }

    public static LocalDateTimeHolder formDate(Long date) {
        LocalDateTime localDateTime = dateIsLong(date);
        return new LocalDateTimeHolder(localDateTime);
    }

    public static LocalDateTimeHolder formDate(String date) {
        LocalDateTime localDateTime = dateIsString(date);
        return new LocalDateTimeHolder(localDateTime);
    }

    /**
     * 从date日期开始往前/后推几天的时间点
     *
     * @param date
     * @param day
     * @return
     */
    public static LocalDateTimeHolder dateBeforeDay(Date date, int day) {
        LocalDateTime localDateTime = dateIsDate(date);
        LocalDateTime beforeDateTime = localDateTime.plusDays(day);
        return new LocalDateTimeHolder(beforeDateTime);
    }

    /**
     * 从date日期开始往前/后推几天的时间点
     *
     * @param date
     * @param day
     * @return
     */
    public static LocalDateTimeHolder dateBeforeDay(Long date, int day) {
        LocalDateTime localDateTime = dateIsLong(date);
        LocalDateTime beforeDateTime = localDateTime.plusDays(day);
        return new LocalDateTimeHolder(beforeDateTime);
    }

    /**
     * 从date日期开始往前/后推几天的时间点
     *
     * @param date
     * @param day
     * @return
     */
    public static LocalDateTimeHolder dateBeforeDay(Timestamp date, int day) {
        LocalDateTime localDateTime = dateIsTimeStamp(date);
        LocalDateTime beforeDateTime = localDateTime.plusDays(day);
        return new LocalDateTimeHolder(beforeDateTime);
    }

    /**
     * 从date日期开始往前/后推几天的时间点
     *
     * @param date
     * @param day
     * @return
     */
    public static LocalDateTimeHolder dateBeforeDay(String date, int day) {
        LocalDateTime localDateTime = dateIsString(date);
        LocalDateTime beforeDateTime = localDateTime.plusDays(day);
        return new LocalDateTimeHolder(beforeDateTime);
    }

    private static LocalDateTime dateIsLong(Long date) {
        return LocalDateTime.ofInstant(Instant.ofEpochMilli(date), ZoneId.systemDefault());
    }

    private static LocalDateTime dateIsDate(Date date) {
        return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
    }

    private static LocalDateTime dateIsTimeStamp(Timestamp date) {
        return date.toLocalDateTime();
    }

    private static LocalDateTime dateIsString(String date) {
        date = date.replaceAll("[- :]", "");
        // 填充日期字符串到14位置 LocalDateTime可以转换
        String localDateTime = StringUtils.rightPad(date, 14, "0");
        return LocalDateTime.parse(localDateTime, DateBaseHolder.getFormatter(DateBaseHolder.Pattern.YMDHMS));
    }

    /**
     * 一天开始 0点
     *
     * @param date
     * @return
     */
    public static LocalDateTimeHolder dateBegin(Date date) {
        LocalDateTime localDateTime = dateIsDate(date);
        return new LocalDateTimeHolder(localDateTime).min();
    }

    public static LocalDateTimeHolder dateBegin(String date) {
        LocalDateTime localDateTime = dateIsString(date);
        return new LocalDateTimeHolder(localDateTime).min();
    }

    public static LocalDateTimeHolder dateBegin(Long date) {
        LocalDateTime localDateTime = dateIsLong(date);
        return new LocalDateTimeHolder(localDateTime).min();
    }

    public static LocalDateTimeHolder dateBegin(Timestamp date) {
        LocalDateTime localDateTime = dateIsTimeStamp(date);
        return new LocalDateTimeHolder(localDateTime).min();
    }

    /**
     * 几天前/后的一天开始时间
     *
     * @param day
     * @return
     */
    public static LocalDateTimeHolder dateBegin(int day) {
        LocalDateTime localDateTime = LocalDateTime.now().plusDays(day);
        return new LocalDateTimeHolder(localDateTime).min();
    }

    /**
     * 指定日期的结束时间 比如 2019.11.19 23.59.59
     *
     * @param date
     * @return
     */
    public static LocalDateTimeHolder dateEnd(Date date) {
        //当天结束时间点
        LocalDateTime localDateTime = dateIsDate(date);
        return new LocalDateTimeHolder(localDateTime).max();
    }

    public static LocalDateTimeHolder dateEnd(Long date) {
        //当天结束时间点
        LocalDateTime localDateTime = dateIsLong(date);
        return new LocalDateTimeHolder(localDateTime).max();
    }

    public static LocalDateTimeHolder dateEnd(String date) {
        //当天结束时间点
        LocalDateTime localDateTime = dateIsString(date);
        return new LocalDateTimeHolder(localDateTime).max();
    }

    public static LocalDateTimeHolder dateEnd(Timestamp date) {
        //当天结束时间点
        LocalDateTime localDateTime = dateIsTimeStamp(date);
        return new LocalDateTimeHolder(localDateTime).max();
    }

    /**
     * 指定天数前/后的结束时间
     * 天结束时间点 23:59:59
     * @param day
     * @return
     */
    public static LocalDateTimeHolder dateEnd(int day) {
//        LocalDateTime localDateTime = now().minusDays(day);
        LocalDateTime localDateTime = now().plusDays(day);
        return new LocalDateTimeHolder(localDateTime).max();
    }

}
